<template>
  <div>
    <BasicTable
      @register="registerTable"
      :can-resize="true"
      :bordered="true"
      :striped="true"
      :loading="true"
    >
      <template #userFlag="{ record }">
        <Tag :color="record.enable === true ? 'green' : 'yellow'">
          {{ record.enable === true ? '启用' : '禁用' }}
        </Tag>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="onAddUser"> 录入数据 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: onEditUser.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: onDeleteUser.bind(null, record),
              },
            },
            // {
            //   icon: 'ant-design:check-circle-outlined',
            //   // onClick: handleSetFlagSuccess.bind(null, record.id),
            // },
            // {
            //   icon: 'ant-design:close-circle-outlined',
            //   // onClick: handleSetFlagFail.bind(null, record.id),
            // },
          ]"
        />
      </template>
    </BasicTable>
    <add @register="registerAddForm" @success="onAddUserSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, provide } from 'vue';
  // import { defineComponent, reactive } from 'vue';
  // import moment from 'moment';
  import { Tag } from 'ant-design-vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  //使用modal弹出窗口
  import { useModal } from '/@/components/Modal';

  import add from './add.vue';

  import { queryUser, deleteUser } from '/@/api/demo/user';
  import { userTableColumns, userQueryConditionFormSchema } from './user.data';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  const { createMessage } = useMessage();

  export default defineComponent({
    name: 'UserManagemenet',
    components: { BasicTable, TableAction, Tag, add },
    setup() {
      //定义模块的名称，提供给子孙组件获取使用
      const moduleName = ref('用户管理');

      const [registerAddForm, { openModal: openAddForm }] = useModal();

      const [registerTable, { reload }] = useTable({
        title: '用户数据管理',
        api: queryUser, //api中指定的为table加载数据对应的查询方法
        columns: userTableColumns,
        formConfig: {
          labelWidth: 80, //设置查询表单中label的宽度
          schemas: userQueryConditionFormSchema,
          showResetButton: false, //查询条件部分中是否显示重置按钮
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: true,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined, //表格操作列是否固定，不设置时默认固定在右侧
        },
      });

      function onAddUser() {
        //使用props传递数据至子组件，在data参数中指定
        openAddForm(true, { op: OperationTypeEnum.ADD });
        // openAddForm(true, {});
      }

      function onEditUser(record: Recordable) {
        openAddForm(true, { op: OperationTypeEnum.EDIT, dataId: record.id });
      }

      function onDeleteUser(record: Recordable) {
        deleteUser(record.id)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            let b1 = res == undefined;
            let b2 = res == null;
            // let b3 = Object.keys(res).length == 0;
            console.log('b1=' + b1 + ' b2=' + b2 + ' b3=');
            if (res != undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              reload();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      function onAddUserSuccess() {
        reload();
      }

      provide('moduleName', moduleName);

      return {
        moduleName,
        registerTable,
        registerAddForm,
        onAddUser,
        onEditUser,
        onDeleteUser,
        onAddUserSuccess,
      };
    },
  });
</script>
